﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using AmazedSaint.Vingy.ViewModels;
using System.Timers;
using EnvDTE80;

namespace AmazedSaint.Vingy
{
    /// <summary>
    /// Interaction logic for MyControl.xaml
    /// </summary>
    public partial class SearchControl : UserControl
    {
        WebSearchViewModel search = new WebSearchViewModel();
        LinearGradientBrush lb;

        public SearchControl()
        {
            InitializeComponent();
            this.DataContext = search;
            search.ResultsModified += (s, a) => this.Dispatcher.Invoke(new Action(scrollView.ScrollToTop),null);
            lb = new LinearGradientBrush(Colors.LightBlue, Colors.White,60);
        
        }

        /// <summary>
        /// Initialize the atuo search based on selected text
        /// </summary>
        public void SearchSelection()
        {
            var dte = VingyPackage.GetGlobalService(typeof(EnvDTE.DTE)) as DTE2;
            if (dte != null && dte.ActiveDocument != null)
            {
                var sel = dte.ActiveDocument.Selection as EnvDTE.TextSelection;
                if (sel != null && !string.IsNullOrEmpty(sel.Text))
                {
                    this.Dispatcher.Invoke(new Action(() =>
                    {
                        if (txtMain.Text != sel.Text)
                        {
                            this.txtMain.Text = sel.Text;
                            this.search.Input = sel.Text;
                            search.DoAsync(() => search.Search());
                        }
                    }
                    ));

                }
            }
        }


        private void Border_MouseEnter(object sender, MouseEventArgs e)
        {
            var border = (sender as Border);
            border.Background =  lb;
            var panel = border.FindName("buttonPanel") as StackPanel;
            panel.Visibility = Visibility.Visible;
        }

        private void Border_MouseLeave(object sender, MouseEventArgs e)
        {
            var border = (sender as Border);
            border.Background = Brushes.White;
            var panel = border.FindName("buttonPanel") as StackPanel;
            panel.Visibility = Visibility.Collapsed;
        }

        private void txtMain_GotFocus(object sender, RoutedEventArgs e)
        {
            cmbSource.Visibility = Visibility.Collapsed;
            txtMain.FontSize = 14;
        }

        private void txtMain_LostFocus(object sender, RoutedEventArgs e)
        {
            cmbSource.Visibility = Visibility.Visible;
            txtMain.Foreground = Brushes.Black;
            txtMain.FontSize = 11;
        }

        private void TextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var result = (sender as TextBlock).DataContext as SearchResult;
            result.OpenCommand.Execute(null);
        }

        private void txtMain_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Return)
            {
                search.DoAsync(() => search.Search());
            }
        }
    }
}